<?php
class Profesor extends CI_Controller{
    private $_userInfo;
    private $_groupSelected;
    
    function __construct() {
        parent::__construct();
        $this->load->model('BlockModel');
        $this->_userInfo = $this->session->userData('userInformation');
        $this->_groupSelected = $this->session->userData('groupSelected');
    }
    
    function index() {
        
        $this->_groupSelected = $this->BlockModel->getGroupByState($this->_userInfo->userId, 'habilitado');
        if(count($this->_groupSelected) == 1){
            $this->session->set_userdata('groupSelected',  $this->_groupSelected[0]);
            $this->showEnabledExam($this->_groupSelected[0]);
        }else{
            $this->loadMainView();
        }
    }
    
    function loadMainView(){
        $data['userInfo'] = $this->_userInfo;
        $data['courses'] = $this->getCourses();
        $this->load->view('TeacherMainView',$data);
    }
    
    public function getCourses() {
        $this->load->model('CourseModel');
        $cursosArray = $this->CourseModel->getCourseByUser($this->_userInfo->userId);
        return $cursosArray;
    }
    
    public function getBlocks() {
        //echo "WHAAAAAAAT";
        $userId = $this->input->post('userId_ajax');
        $courseId = $this->input->post('courseId_ajax');
        $blocksCollection = $this->BlockModel->getBlocksByUserAndCourse($userId, $courseId);
        echo json_encode($blocksCollection); 
    }
    
    public function enableExam() {
        
        $courseId = $this->input->post('courseSelectInput');
        $blockId = $this->input->post('blockSelectInput');
        
        // UPDATE AL ESTADO DEL PROFESOR - CURSO - BLOQUE
        $groupId = $this->BlockModel->getGroupId($courseId, $blockId);
        $this->BlockModel->updateBlockState($groupId, 'habilitado');
        
        $groups = $this->BlockModel->getGroupByState($this->_userInfo->userId, 'habilitado');
        $this->showEnabledExam($groups[0]);
       
    }
    
    public function showEnabledExam($group) {
        $data['group'] = $group;
        $this->load->view('EnabledExamView',$data);
    }
    
    public function disableExam() {
        //echo var_dump($this->_groupSelected);
        $groupId = $this->_groupSelected->groupId;
        $this->BlockModel->updateBlockState($groupId, 'deshabilitado');
        redirect('http://localhost/FileCollector/Profesor');  
    }
}
